/**
 * @file Network.DLL.h
 *
 * @ingroup	libNetwork
 * @brief	structures for data link layer
 *			most parts extracted from ScatterWeb.Net.h
 * @author	Andreas Schlueter <mersaultpatrice@gmail.com>
 * @date	Mar 2008
 * @version	0.5
 */

#ifndef NETWORK_DLL_H
#define NETWORK_DLL_H

#include "Network.Net.h"

#define FRAME_SIZE		128     ///< size of a network frame
#define PREAMBLE_SIZE	9       ///< preamble and sync word sent before each packet
#define DIALOG_MSG_SIZE 64		///< maximum size of dialog message

/**
 * size of protocol overhead
 */
#define DLL_HEADER_SIZE	2 ///< size of data link header
#define OVERHEAD	(DLL_HEADER_SIZE + NET_HEADER_SIZE + TP_HEADER_SIZE)


/**
 * @brief	Network Address
 */
typedef uint8_t		netaddr_t;

/**
 * @brief	Predefined network addresses
 */
enum netaddr_predef {
	NETADDR_UNDEFINED	= 0x00,		///< reserved for undefined (not set) or to address "this" node if a node is addressed through a wire connection
	NETADDR_MIN		= 0x01,		///< smallest possible node address
	NETADDR_MAX		= 0xFE,		///< largest possible node address
	NETADDR_BROADCAST	= 0xFF		///< broadcast address
};

/**
 * @brief	dll header
 * 
 * @note	First two fields (size and destination) conform to CC1100
 * 			MAC packet format.
 */
__attribute__((packed))typedef struct{
	uint8_t		size;			///< size of packet	
	uint8_t		type;			///< protocol id
	uint8_t		from;			///< source address (will be discarded in final version)
} frame_header_t;


/**
 * @brief	dll header
 * 
 * @note	First two fields (size and destination) conform to CC1100
 * 			MAC packet format.
 */
__attribute__((packed))typedef struct{
	uint8_t		size;			///< size of packet	
	uint8_t		type;			///< protocol id
} frame_header_t2;


/**
 * data frame with preambel, header and payload
 *
 * 
 * @NOTE  size of entire structure must be even!
 */
typedef union{
  uint8_t raw[FRAME_SIZE];
  struct{
    uint8_t			preamble[PREAMBLE_SIZE];			///< preamble: AA AA AA AA AA AA D3 91 00
	frame_header_t2 nwh __attribute__((packed));		///< Network header
	packet_d3_t		payload __attribute__((packed));	///< data
	uint16_t		crc;								///< 16bit CRC (asure even address!)
	uint16_t		epilogue;							///< 2byte buffer avoids transmission error (asure even address!)
  }decoded;
}frame_t;

/**
 * full frame with preambel, header and payload
 */
__attribute__((packed)) typedef union{
  uint8_t raw[DIALOG_MSG_SIZE];
  struct{
    uint8_t     preamble[PREAMBLE_SIZE];///< preamble: AA AA AA AA AA AA D3 91 00
    frame_header_t nwh; ///< Network header
    uint8_t* payload[DIALOG_MSG_SIZE- sizeof(frame_header_t) - PREAMBLE_SIZE];///< data
  }decoded;
}short_frame_t;

#endif




